<template>
  <div class="main-content">
    <!-- 列表页 -->
    <div v-if="showFlag">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-row :gutter="20" class="slt">
          <el-form-item label="体质类型">
            <el-input prefix-icon="el-icon-search" v-model="searchForm.tizhileixing" placeholder="体质类型"
                      clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="success" @click="search()">查询</el-button>
          </el-form-item>
        </el-row>

        <el-row class="ad">
          <el-form-item>
            <el-button
                type="success"
                icon="el-icon-plus"
                @click="addOrUpdateHandler()"
            >新增
            </el-button>
            <el-button
                :disabled="dataListSelections.length <= 0"
                type="danger"
                icon="el-icon-delete"
                @click="deleteHandler()"
            >删除
            </el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <div class="table-content">
        <el-table class="tables"
                  :size="'medium'"
                  :show-header="true"
                  :border="true"
                  :fit="true"
                  :stripe="true"
                  :style="{width: '100%',fontSize:'14px',color:'#606266'}"
                  v-if="true"
                  :data="dataList"
                  v-loading="dataListLoading"
                  highlight-current-row
                  @selection-change="selectionChangeHandler">
          <el-table-column
              type="selection"
              align="center"
              width="50">
          </el-table-column>
          <el-table-column
              label="索引"
              align="center"
              type="index"
              width="50"/>
          <el-table-column
              align="center"
              prop="tizhileixing"
              header-align="center"
              label="体质类型">
            <template slot-scope="scope">
              {{ scope.row.tizhileixing }}
            </template>
          </el-table-column>
          <el-table-column
              width="360"
              align="center"
              header-align="center"
              label="操作"
              fixed="right"
              class-name="operation-column">
            <template slot-scope="scope">
              <div class="operation-buttons">
                <el-button
                    type="success"
                    icon="el-icon-tickets"
                    size="mini"
                    @click="addOrUpdateHandler(scope.row.id, 'info')">
                  详情
                </el-button>
                <el-button
                    type="primary"
                    icon="el-icon-edit"
                    size="mini"
                    @click="addOrUpdateHandler(scope.row.id)">
                  修改
                </el-button>
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    @click="deleteHandler(scope.row.id)">
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            clsss="pages"
            :layout="layouts"
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            :total="totalPage"
            class="pagination-content"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
    <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>
  </div>
</template>
<script>
import axios from 'axios'
import AddOrUpdate from "./add-or-update";

export default {
  data() {
    return {
      searchForm: {
        key: ""
      },
      form: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag: false,
      layouts: 'total, sizes, prev, pager, next, jumper',
    };
  },
  created() {
    this.init();
    this.getDataList();
  },
  mounted() {

  },
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g, '');
    }
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    init() {

    },
    // 搜索
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
      }
      if (this.searchForm.tizhileixing !== '' && this.searchForm.tizhileixing !== undefined) {
        params['tizhileixing'] = '%' + this.searchForm.tizhileixing + '%'
      }
      this.$http({
        url: "tizhileixing/page",
        method: "get",
        params: params
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id, type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if (type !== 'info') {
        type = 'else';
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, type);
      });
    },
    // 查看评论
    disscussListHandler(id) {
      this.$router.push({path: '/discusstizhileixing', query: {refid: id}});
    },
    // 下载
    download(file) {
      window.open(`${file}`)
    },
    // 删除
    deleteHandler(id) {
      var ids = id
          ? [Number(id)]
          : this.dataListSelections.map(item => {
            return Number(item.id);
          });
      this.$confirm(`确定进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "tizhileixing/delete",
          method: "post",
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.search();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },


  }

};
</script>
<style lang="scss" scoped>
.main-content {
  padding: 10px;
}

.search-form {
  background-color: #fff;
  border-radius: 6px;
  padding: 10px 20px;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.1);
  margin-bottom: 10px;
}

.table-content {
  background-color: #fff;
  border-radius: 6px;
  padding: 10px;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.1);
}

.slt {
  margin: 0 !important;
  display: flex;
}

.ad {
  margin: 0 !important;
  display: flex;
}

.pages {
  & /deep/ .el-pagination__sizes {
    & /deep/ .el-input__inner {
      height: 22px;
      line-height: 22px;
    }
  }
}

.el-button + .el-button {
  margin-left: 10px;
}

.tables {
  .el-button {
    padding: 6px 10px;
    font-size: 12px;
    margin: 3px;
    min-width: 70px;
    border-radius: 3px;

    &.el-button--success {
      background-color: #4a90e2;
      border-color: #4a90e2;
    }

    &.el-button--primary {
      background-color: #4a90e2;
      border-color: #4a90e2;
    }

    &.el-button--danger {
      background-color: #ff3b30;
      border-color: #ff3b30;
    }

    i {
      margin-right: 3px;
    }
  }
}

.operation-column {
  .operation-buttons {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
  }
}

.pagination-content {
  margin-top: 15px;
  display: flex;
  justify-content: center;
}
</style>
